package com.pomelo.tiku.impls;

import android.content.Context;

import com.pomelo.tiku.beans.RegisterBean;
import com.pomelo.tiku.beans.ResponseData;
import com.pomelo.tiku.http.OkHttpProcessor;
import com.pomelo.tiku.interfaces.Contract;
import com.pomelo.tiku.interfaces.IHttpCallBack;
import com.pomelo.tiku.utils.GsonUtil;

import java.util.HashMap;

/**
 * @author liuxiangwang
 * @date .
 */
public class RegisterModel implements Contract.IRegisterModel {
    private Context mContext;
    public RegisterModel(Context context) {
        this.mContext = context;
    }
    @Override
    public void register(String url, HashMap<String,String> header,HashMap<String,String> data,Boolean isFormBody,IRegisterResult listener) {
        OkHttpProcessor.getInstance(mContext).postRequest(url, header, data, isFormBody, new IHttpCallBack() {
            @Override
            public void onStart() {
                if(listener !=null) {
                    listener.onStart();
                }
            }

            @Override
            public void onSuccess(ResponseData data) {
                if(listener != null) {
                    RegisterBean registerBean = GsonUtil.parseJsonToBean(data.getData().toString(),RegisterBean.class);
                    if(registerBean != null) {
                        listener.onSuccess(registerBean);
                    } else {
                        listener.onFailed("服务器返回数据格式有误");
                    }
                }
            }

            @Override
            public void onFailed(int code, String message) {
                if(listener != null) {
                    listener.onFailed(message);
                }
            }

            @Override
            public void onHttpFailed(String message) {
                if(listener != null) {
                    listener.onHttpFailed(message);
                }
            }
        });
    }
}
